package com.wbk.lotterysystemlotteryservice.service.serviceImpl;

import com.wbk.lotterysystemlotteryservice.commons.bo.LotteryBO;
import com.wbk.lotterysystemlotteryservice.entity.Lottery;
import com.wbk.lotterysystemlotteryservice.entity.LotteryExample;
import com.wbk.lotterysystemlotteryservice.mapper.LotteryMapper;
import com.wbk.lotterysystemlotteryservice.service.LotteryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LotteryServiceImpl implements LotteryService {

    @Autowired
    LotteryMapper lotteryMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Lottery save(LotteryBO lotteryBO) {
        Lottery lottery = new Lottery();
        BeanUtils.copyProperties(lotteryBO, lottery);
        lottery.setLotteryStatus((byte) 0);
        lottery.setAuditStatus((byte) 0);
        lotteryMapper.insertSelective(lottery);
        return lottery;
    }

    @Cacheable(cacheNames = "lottery::byId", key = "#id")
    @Override
    public Lottery getLotteryById(Integer id) {
        return lotteryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Lottery> getAll() {
        LotteryExample lotteryExample = new LotteryExample();
        LotteryExample.Criteria criteria = lotteryExample.createCriteria();
        criteria.andAuditStatusEqualTo((byte) 1);
        lotteryExample.setOrderByClause("start_time desc , due_time asc");
        return lotteryMapper.selectByExample(lotteryExample);
    }

    @Override
    public List<Lottery> getAllWithoutAudit() {
        LotteryExample lotteryExample = new LotteryExample();
        LotteryExample.Criteria criteria = lotteryExample.createCriteria();
        criteria.andAuditStatusEqualTo((byte) 0);
        lotteryExample.setOrderByClause("start_time desc , due_time asc");
        return lotteryMapper.selectByExample(lotteryExample);
    }

    @CacheEvict(cacheNames = "lottery::byId", key = "#lottery.id")
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int update(Lottery lottery) {
        return lotteryMapper.updateByPrimaryKeySelective(lottery);
    }

    @CacheEvict(cacheNames = "lottery::byId", key = "#lottery.id")
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int audit(Lottery lottery) {
        return lotteryMapper.updateByPrimaryKeySelective(lottery);
    }

    @Override
    public List<Lottery> getAllByStoreUserNo(Long storeUserNo) {
        LotteryExample lotteryExample = new LotteryExample();
        LotteryExample.Criteria criteria = lotteryExample.createCriteria();
        criteria.andStoreUserNoEqualTo(storeUserNo);
        lotteryExample.setOrderByClause("audit_status desc  , start_time desc , due_time asc");
        return lotteryMapper.selectByExample(lotteryExample);
    }

    @Override
    public List<Lottery> getAllHasJoin(List<Integer> ids) {
        LotteryExample lotteryExample = new LotteryExample();
        LotteryExample.Criteria criteria = lotteryExample.createCriteria();
        criteria.andIdIn(ids);
        lotteryExample.setOrderByClause("lottery_status desc  , start_time desc , due_time asc");
        return lotteryMapper.selectByExample(lotteryExample);
    }
}
